﻿using Common;
using Entity;
using Microsoft.AspNetCore.Mvc;
using Renovation.Models;

namespace Renovation.Controllers
{
    public class RoleInfoController : Controller
    {
        /// <summary>
        /// 角色列表页面
        /// </summary>
        /// <returns></returns>
        public ViewResult RoleInfoListView()
        {
            return View();
        }
        /// <summary>
        /// 角色添加页面
        /// </summary>
        /// <returns></returns>
        public ViewResult CreateRoleInfoView()
        {
            return View();
        }
        /// <summary>
        /// 修改角色页面
        /// </summary>
        /// <returns></returns>
        public ViewResult UpdateRoleInfoView()
        {
            return View();
        }
        /// <summary>
        /// 添加角色
        /// </summary>
        /// <returns></returns>
        public APIResultModel CreateRoleInfo(string name, string remark)
        {
            //参数验证
            if (string.IsNullOrEmpty(name))
            {
                return APIResultModel.GetErrorResult("请输入角色名称");
            }
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            //创建角色
            RoleInfo roleInfo = new RoleInfo();
            roleInfo.Id = Guid.NewGuid().ToString();
            roleInfo.Name = name;
            roleInfo.Remark = remark;
            roleInfo.CreateTime = DateTime.Now;
            
            //向数据集添加
            dbContext.RoleInfo.Add(roleInfo);
            //保存
            dbContext.SaveChanges();

            return APIResultModel.GetSuccessResult("注册成功");
        }
        /// <summary>
        /// 获取一个角色的数据
        /// </summary>
        /// <returns></returns>
        public APIResultModel GetRoleInfo(string id)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            //查看一条数据
            RoleInfo RoleInfo = dbContext.RoleInfo.FirstOrDefault(e => e.Id == id);
            return APIResultModel.GetSuccessResult("成功", RoleInfo);
        }
        /// <summary>
        /// 获取多条数据
        /// </summary>
        /// <returns></returns>
        public object GetRoleInfoList(int page, int limit, string name)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();
            
            //还没有查询出来的数据集
            IQueryable<RoleInfo> list = dbContext.RoleInfo.Where(e => e.IsDelete == false);
            //name有数据
            if (!string.IsNullOrEmpty(name))
            {

                list = list.Where(e => e.Name.Contains(name));
            }
            
            //先排序再分页，OrderBy升序，OrderByDescending降序
            List<RoleInfoModel> res = list.OrderByDescending(e => e.CreateTime).Skip((page - 1) * limit).Take(limit).Select(e => new RoleInfoModel()
            {
                Id = e.Id,
                Name = e.Name,
                Remark = e.Remark,
                CreateTime = e.CreateTime.ToString("yyyy-MM-dd hh:mm:ss"),
            }).ToList();
            //获取一共有多少条数据
            int count = list.Count();
            return APIResultModel.GetSuccessResultForLayui("成功", count, res);

        }

        /// <summary>
        /// 删除一个角色
        /// </summary>
        /// <returns></returns>
        public APIResultModel DeleteRoleInfo(string id)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();
        
            //先查询角色
            RoleInfo roleInfo = dbContext.RoleInfo.FirstOrDefault(e => e.Id == id);

            if (roleInfo != null)
            {
                //角色存在删除
                roleInfo.IsDelete = true;
                roleInfo.DeleteTime = DateTime.Now;
                //保存
                dbContext.SaveChanges();

                return APIResultModel.GetSuccessResult("成功");
            }
            else
            {
                //角色不存在报错
                return APIResultModel.GetErrorResult("失败");
            }


        }
        /// <summary>
        /// 批量删除角色
        /// </summary>
        /// <param name="ids"></param>
        /// <returns></returns>
        public APIResultModel BatchDeleteRoleInfo(List<string> ids)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();

            foreach (var id in ids)
            {
                //先查询角色是否存在
                RoleInfo roleInfo = dbContext.RoleInfo.FirstOrDefault(e => e.Id == id);

                if (roleInfo != null)
                {
                    roleInfo.IsDelete = true;
                    roleInfo.DeleteTime = DateTime.Now;
                }
            }
            //保存
            int index = dbContext.SaveChanges();
            return APIResultModel.GetSuccessResult("成功");
        }
        /// <summary>
        /// 修改角色
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public APIResultModel UpdateRoleInfo(string id, string remark)
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();
            //先查询角色
            RoleInfo roleInfo = dbContext.RoleInfo.FirstOrDefault(e => e.Id == id);

            if (roleInfo != null)
            {
                //角色存在修改
                roleInfo.Remark = remark;
                int index = dbContext.SaveChanges();
                return APIResultModel.GetSuccessResult("成功");
            }
            else
            {
                //角色不存在报错
                return APIResultModel.GetSuccessResult("失败");
            }

        }
        /// <summary>
        /// 获取角色下拉选择的集合
        /// </summary>
        /// <returns></returns>
        public APIResultModel GetSelectOptions()
        {
            //创建数据库上下文类型实例
            MyDbContext dbContext = new MyDbContext();
            var res = dbContext.RoleInfo.Where(e => e.IsDelete == false).Select(r => new
            {
                Title = r.Name,
                Value = r.Id
            }).ToList();
            return APIResultModel.GetSuccessResult("成功",res);
        }
    }
}
